
import { ChangeEvent } from 'react'
import { Input, InputProps } from 'antd'
import './forms.scss'
import { useFormItemInit } from './hooks';
import { FormItemBaseProps } from './interface';
import React from 'react';
import { useDrag } from './drop.hooks';
import { Utils } from '../../utils/utils';
import { Components } from '../..';




interface IProps extends FormItemBaseProps {
}
export default function MyPassword(props: IProps) {
    const { formControl, className = '', style={} } = props;
    const {
        value, setValue,
        readonly,
        outerClassName,
        layout,
        showError,
        outerStyle,
        labelStyle,
        selectorStyle,
    } = useFormItemInit(formControl, className, style);

    const onInputChange = (e: ChangeEvent<HTMLInputElement>) => {
        const val = e.target.value;
        setValue(val);
        formControl.setValue(val);
    };

    const inputProps: InputProps = {
        onChange: onInputChange,
        prefix: formControl.controlData.prefix,
        allowClear: false,
        size: formControl.controlData.size ?? 'middle', // large | middle | small
        className: 'input',
        value: value,
        placeholder: formControl.controlData.placeholder || '请输入',
        maxLength: formControl.controlData.inputMaxlength,
        disabled: readonly,
        status: showError ? 'error' : undefined,
        styles:{'input':selectorStyle},
        onPressEnter: (e) => {
            e.stopPropagation();
            if (Utils.isFunction(formControl.onPressEnter)) {
                formControl.onPressEnter!(e);
            }
        },
    }

    /** 拖拽排序 */
    const dropRef = React.useRef<HTMLDivElement>(null);
    useDrag({ dropRef, formControl });

    return (
        <div component-name='form-item' className={outerClassName} style={outerStyle} ref={dropRef}>
            <div className={`field ${layout}`}>
                <Components.forms.FormLabel formControl={formControl} style={labelStyle} />
                <div className='control'>
                    <div className="value">
                        <Input.Password {...inputProps} visibilityToggle={true} />
                        {/* <div className='error-txt'>必填</div> */}
                    </div>
                </div>
            </div>
        </div>
    )
}
